﻿using Lis.DTO;
using Lis.IBLL;
using Lis.IRepository;
using Lis.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lis.BLL
{
    public class ReturnBookService : BaseService<BorrowBookDto>, IReturnBookService
    {
        #region 01，还书+Model.ServiceResult<string> ReturnBook(BorrowBookDto borrow)
        /// <summary>
        /// 还书
        /// </summary>
        /// <param name="borrow">实体模型</param>
        /// <returns></returns>
        public Model.ServiceResult<string> ReturnBook(BorrowBookDto borrow)
        {

            //1，判断该书有没有被预约
            //2，日志表里面有插数据
            //3，读者典藏表里面删除数据
            var Message = "还书成功";
            #region 1，判断图书有没有被预约   通过
            var book_YY = ((IAppointmentInfoRepository)DBSession.SysAppointmentInfoRepository).GetEntitiesByBarCode(borrow.BarCode).Where<AppointmentInfo>(u => u.DSRQ != null && u.JCRQ != null).ToList();

            //其他人预约
            book_YY = book_YY.AsEnumerable().Where<AppointmentInfo>(u => DateTime.Parse(u.DSRQ).AddDays(CirculateConfig.AppointHoldDay) > DateTime.Now).ToList();

            if (book_YY != null && book_YY.Count() > 0)//有人预约
            {
                Message = "该图书以被预约，不能进行借阅操作";
            }
            #endregion


            //var book_WJ=((ICirculateInfoRepository)DBSession.SysCirculateInfoRepository).

            bool exist = false;
            #region 12，日志表里面插入还书记录
            //12，日志表里面插入还书记录
            var logReturn = new LogInfo()
            {
                DZTM = borrow.UserName,
                DCTM = borrow.BarCode,
                CZY = CirculateConfig.Operator,
                CZRQ = DateTime.Now,
                CZLXDM = "HH",
                SQLSTR = @"Delete from dzdcb where dctm='" + borrow.BarCode + "'",
                LYB = "zydck",
                MARC = "U",
                GCDWDM = "1"
            };
            if (DBSession.SysLogInfoRepository.Add(logReturn))
            #endregion
            {
                #region 13，DCDCK删除还书人借书信息   在Dal里面重新开方法
                var circulateInfo = new CirculateInfo()
                {
                    DZTM = borrow.ReturnUserName,
                    DCTM = borrow.BarCode,
                };
                if (DBSession.SysCirculateInfoRepository.Delete(circulateInfo))
                {
                    exist = true;
                }
                #endregion
            }

            throw new NotImplementedException();
        }
        #endregion

        public override IQueryable<BorrowBookDto> GetPageEntities<S>(int pageSize, int pageIndex, out int total, bool isAsc, System.Linq.Expressions.Expression<Func<BorrowBookDto, bool>> whereLambda, System.Linq.Expressions.Expression<Func<BorrowBookDto, S>> orderByLambda)
        {
            throw new NotImplementedException();
        }

        public override bool Add(BorrowBookDto entity)
        {
            throw new NotImplementedException();
        }

        public override bool UpDate(BorrowBookDto entity)
        {
            throw new NotImplementedException();
        }

        public override bool Delete(BorrowBookDto entity)
        {
            throw new NotImplementedException();
        }
    }
}
